﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using BOMS.Utility;
using BOMS.DBUtility;

namespace BOMS.DAL
{
    public class RealDate
    {
        public static DataSet getWinceInfoOfLan()
        {
            /*
            DataSet ds = new DataSet();
            //获取所有现场设备信息，和最后通讯的时间点
            string str_Sql = @"select L.*,isnull(S.LogTime,'2000-1-1') as LatestTime,capFree
                 from M_SubLans L left outer join dbo.S_BatgrpSnapshot S
                on L.Mac=S.EquipID where L.isDel='n' order by S.LogTime desc";
            string str_MySql = @"select L.*,S.LogTime as LatestTime,capFree
                 from M_SubLans L left outer join S_BatgrpSnapshot S
                on L.Mac=S.EquipID where L.isDel='n' order by S.LogTime desc";
            */
            DataSet ds = null;

            string str_Sql ;
            
            

            if (DBUtility.DbHelper.DBType == "MsSql")
            {
                str_Sql = @"select L.*,isnull(s.LogTime,'2000-1-1') as LatestTime,capFree
                 from M_SubLans l                  
                 inner join S_BatgrpSnapshot s on  l.Mac=s.EquipID
                 inner join (select max(ID) as ID from S_BatgrpSnapshot group by EquipID) t 
                on s.ID=T.ID where l.isDel='n'                 
                order by s.LogTime desc";

                ds = DBUtility.DbHelper.Query(str_Sql);
            }
            else if (DBUtility.DbHelper.DBType == "MySql")
            {
                str_Sql = @"select L.*,ifnull(s.LogTime,cast('2000-1-1' as datetime)) as LatestTime,capFree
                 from M_SubLans l                  
                 inner join S_BatgrpSnapshot s on  l.Mac=s.EquipID
                 inner join (select max(ID) as ID from S_BatgrpSnapshot group by EquipID) t 
                on s.ID=T.ID where l.isDel='n'                 
                order by s.LogTime desc";

                ds = DBUtility.DbHelper.Query(str_Sql);
            }

            return ds;
        }

        public static int getPowerDownEquipCount()
        {
            string str_Sql = @"select count(1) from PowerControl where BatGrpDscV=0.00";
            Object obj = DBUtility.DbHelper.GetSingle(str_Sql);
            return Convert.ToInt32(obj.ToString());
        }

        public static int getWarningEquipCount()
        {
            string str_Sql = @"select count(1) from S_Warning where IsActive=1";
            Object obj = DBUtility.DbHelper.GetSingle(str_Sql);
            return Convert.ToInt32(obj.ToString());
        }

        public static DataSet getGroupInfoByWinceIp(string ip)
        {
            string str_Sql = "select * from s_batgrpsnapshot where EquipID='" + ip + "'";
            DataSet ds = DBUtility.DbHelper.Query(str_Sql);
            return ds;
        }

        public static DataSet getBatteryInfoByGroupId(string winceIp, string id)
        {
            string str_Sql = "select * from S_BatSnapshot where EquipID='" + winceIp + "' and batterygroup='" + id + "'";
            DataSet ds = DBUtility.DbHelper.Query(str_Sql);
            return ds;
        }

    }
}
